var $ = require("../../utils/Ajax.js");
Page({
  data: {
    // 图片路径配置
    PictureUrl: getApp().PictureUrl,

    // 我的存茶
    TeaData0: [],

    // 转卖中
    TeaData1: [],

    // 已卖出
    TeaData2: [],

    // 销量 - 上架时间
    Condition: [
      { type: "1", name: "我的存茶" },
      { type: "2", name: "转卖中" },
      { type: "3", name: "已卖出" }
    ],

    // 销量 - 上架时间下标
    Index: 0,

  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "我的存茶"
    })
  },
  onShow: function(){
    var that = this;
    $.Ajax("api/myTea", "GET", {}, function (res) {
      if (res.s) {
        that.setData({
          TeaData0: res.d.mytea
        })
      } else {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  // 点击我的存茶 / 转卖中 / 已卖出
  Condition: function (e) {
    var that = this;
    var Index = e.currentTarget.dataset.index_;
    this.setData({
      Index: Index
    })
    switch(Index){
      case 0:
        $.Ajax("api/myTea", "GET", {}, function (res) {
          if (res.s) {
            that.setData({
              TeaData0: res.d.mytea
            })
          } else {
            wx.showToast({
              title: res.m,
              icon: 'none',
              duration: 2000
            })
          }
        })
        break;
      case 1:
        $.Ajax("api/mySaleTea", "GET", {}, function (res) {
          console.log(res)
          if (res.s) {
            that.setData({
              TeaData1: res.d.mysaletea
            })
          } else {
            wx.showToast({
              title: res.m,
              icon: 'none',
              duration: 2000
            })
          }
        })
        break;
      case 2:
        $.Ajax("api/mySoleTea", "GET", {}, function (res) {
          console.log(res)
          if (res.s) {
            that.setData({
              TeaData2: res.d.mysoletea
            })
          } else {
            wx.showToast({
              title: res.m,
              icon: 'none',
              duration: 2000
            })
          }
        })
        break;
    }
  },

  // 点击进入商品详情
  TeaDetails: function (e) {
    var Id = e.currentTarget.dataset.id;
    var Type = e.currentTarget.dataset.type;
    var GoodsId = e.currentTarget.dataset.goodsid;
    var stock = e.currentTarget.dataset.stock;
    if (stock >= 1){
      wx.navigateTo({
        url: '../TeaDetails/TeaDetails?Id=' + Id + "&Type=" + Type + "&GoodsId=" + GoodsId,
      })
    }else{
      wx.showToast({
        title: "库存不足,禁止操作此商品!",
        icon: 'none',
        duration: 2000
      })
    }
  }
})